package org.tst.controller;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JEditorPane;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import org.tst.service.ActionInVoker;
import org.tst.service.BaseAction;

public class PaserTset extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	JFileChooser chooser = new JFileChooser();

	private JPanel contentPane;

	private List<String> msgIds = new ArrayList<String>();
	private JEditorPane dtrpnDsds;
	private JComboBox county;
	private JComboBox action;

	private PassDTO passDTO = new PassDTO();
	private JComboBox siteMenu;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PaserTset frame = new PaserTset();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PaserTset() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		dtrpnDsds = new JEditorPane();
		dtrpnDsds.setAutoscrolls(true);
		contentPane.add(dtrpnDsds, BorderLayout.CENTER);

		county = new JComboBox();
		county.setModel(new DefaultComboBoxModel(Country.values()));
		contentPane.add(county, BorderLayout.NORTH);

		action = new JComboBox();
		action.setModel(new DefaultComboBoxModel(Action.values()));
		contentPane.add(action, BorderLayout.SOUTH);

		JButton btnNewButton = new JButton("New button");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String[] out = dtrpnDsds.getText().split("\n");

				Action pAction = (Action) action.getSelectedItem();
				Country pCounty = (Country) county.getSelectedItem();

				Site pSite = (Site) siteMenu.getSelectedItem();

				System.out.println("aciont=" + pAction);
				System.out.println("count=" + pCounty);
				System.out.println("Site=" + pSite);

				passDTO.getPos().clear();
				for (String o : out) {
					PassMsg psm = new PassMsg();

					final BaseAction action = ActionInVoker.get(//
							pAction, pCounty, pSite, o);

					psm.setMsg(o.replace("\r", "").replace("\n", ""));
					psm.setContent(action.doTask());
					final String contet = psm.getContent();
					if (contet.matches("status=2")) {
						psm.setStatus("成功");
					} else if (contet.matches("status=21")) {
						psm.setStatus("pass");
					} else if (contet.matches("status=18")) {
						psm.setStatus("ball out");
					} else if (contet.matches("status=3")) {
						psm.setStatus("faill");
					}
					passDTO.getPos().add(psm);
				}
				File folder = new File("C:\\passOrTryFolder");
				if (folder.exists()) {
					folder.delete();
					folder.mkdirs();
				} else {
					folder.mkdirs();

				}
				for (PassMsg pm : passDTO.getPos()) {
					writeFile(pm);
				}
				writeFile(passDTO.getPos());

			}
		});
		contentPane.add(btnNewButton, BorderLayout.EAST);

		siteMenu = new JComboBox();
		siteMenu.setModel(new DefaultComboBoxModel(Site.values()));
		contentPane.add(siteMenu, BorderLayout.WEST);

	}

	private void writeFile(List<PassMsg> msgs) {
		File file = new File("C:\\passOrTryFolder\\index.txt");
		try {
			file.createNewFile();

			FileWriter fileWriter = new FileWriter(file, true);

			for (PassMsg pm : msgs) {
				fileWriter.append(pm.getMsg() + "-----\t" + pm.getStatus()
						+ "\n");
				fileWriter.append("\n");
			}

			fileWriter.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unused")
	private void writeFile(PassMsg msg) {

		File file = new File("C:/passOrTryFolder/" + msg.getMsg() + ".txt");
		try {
			OutputStreamWriter write = new OutputStreamWriter(
					new FileOutputStream(file), "UTF-8");
			BufferedWriter writer = new BufferedWriter(write);
			// PrintWriter writer = new PrintWriter(new BufferedWriter(new
			// FileWriter(filePathAndName)));
			// PrintWriter writer = new PrintWriter(new
			// FileWriter(filePathAndName));
			writer.write(msg.getContent());
			writer.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
